import { isString, isEmpty } from "@pureadmin/utils";
import { useMultiTagsStoreHook } from "@/store/modules/multiTags";
import {
  useRouter,
  useRoute,
  type LocationQueryRaw,
  type RouteParamsRaw
} from "vue-router";

export function useDetail() {
  const route = useRoute();
  const router = useRouter();
  const getParameter = isEmpty(route.params) ? route.query : route.params;

  function toDetail(
    parameter: LocationQueryRaw | RouteParamsRaw,
    model:
      | "addContract"
      | "editContract"
      | "editContractDetail"
      | "addSupContract"
      | "editSupContract"
      | "editSupContractDetail"
      | "viewContract"
  ) {
    // 传参转字符串：vue-router特性
    Object.keys(parameter).forEach(param => {
      if (!isString(parameter[param])) {
        parameter[param] = parameter[param].toString();
      }
    });
    // 新建 - 主合同
    if (model === "addContract") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_sales/components/editSalesContract",
        name: "editSalesContract",
        query: parameter,
        meta: {
          title: `【新建】销售合同`
        }
      });
      router.push({ name: "editSalesContract", query: parameter });
    }
    // 过审编辑 - 主合同
    else if (model === "editContract") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_sales/components/editSalesContract",
        name: "editSalesContract",
        query: parameter,
        meta: {
          title: `【过审编辑】${parameter.title}`
        }
      });
      router.push({ name: "editSalesContract", query: parameter });
    }
    // 编辑 - 主合同
    else if (model === "editContractDetail") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_sales/components/editSalesContract",
        name: "editSalesContract",
        query: parameter,
        meta: {
          title: `【编辑】${parameter.title}`
        }
      });
      router.push({ name: "editSalesContract", query: parameter });
    }
    // 新建 - 补充合同
    else if (model === "addSupContract") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_sales/components/editSupSalesContract",
        name: "editSupSalesContract",
        query: parameter,
        meta: {
          title: `【新建】销售补充合同`
        }
      });
      router.push({ name: "editSupSalesContract", query: parameter });
    }
    // 过审编辑 - 补充合同
    else if (model === "editSupContract") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_sales/components/editSupSalesContract",
        name: "editSupSalesContract",
        query: parameter,
        meta: {
          title: `【过审编辑】${parameter.title}`
        }
      });
      router.push({ name: "editSupSalesContract", query: parameter });
    }
    // 编辑 - 补充合同
    else if (model === "editSupContractDetail") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_sales/components/editSupSalesContract",
        name: "editSupSalesContract",
        query: parameter,
        meta: {
          title: `【编辑】${parameter.title}`
        }
      });
      router.push({ name: "editSupSalesContract", query: parameter });
    }
    // 查看
    else if (model === "viewContract") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_sales/components/salesContractDetail",
        name: "salesContractDetail",
        query: parameter,
        meta: {
          title: `【查看】${parameter.title}`
        }
      });
      router.push({ name: "salesContractDetail", query: parameter });
    }
  }

  // 用于页面刷新，重新获取浏览器地址栏参数并保存到标签页
  const initToDetail = (
    model:
      | "addContract"
      | "editContract"
      | "editContractDetail"
      | "addSupContract"
      | "editSupContract"
      | "editSupContractDetail"
      | "viewContract"
  ) => {
    if (getParameter) toDetail(getParameter, model);
  };

  return { toDetail, initToDetail, getParameter, router };
}
